package top.toptimus.dto.qcDto;

import com.google.common.base.Strings;
import lombok.Data;
import lombok.NoArgsConstructor;
import top.toptimus.util.ResourcesUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 服装款式信息
 *
 * @author 于辉
 */
@NoArgsConstructor
@Data
public class StyleOptionEntity implements Cloneable {


    @Override
    public StyleOptionEntity clone() {
        StyleOptionEntity stu = null;
        try {
            stu = (StyleOptionEntity) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return stu;
    }

    //总料
    private List<StyleOptionEntity> styleOptionList;
    //原料
    private List<StyleOptionEntity> styleOptionFabricList;
    //扣
    private List<StyleOptionEntity> styleOptionSBUList;
    //洗涤标
    private List<StyleOptionEntity> styleOptionCALBList;
    //商标
    private List<StyleOptionEntity> styleOptionLPList;
    //小号
    private List<StyleOptionEntity> styleOptionSIZELBList;
    //辅料
    private List<StyleOptionEntity> styleOptionSCONList;

    //
    private long qcId;

    //自带面料区分
    private int materialSource;

    // 质检项目id
    private String qcItemId;

    // 工序id
    private String qcOperatingId;

    // 项目code
    private String itemCode;

    // 项目value
    private String itemValue;

    // 项目实际值
    private String optionValue;

    // cost1
    private String optionCost1;

    // 项目名
    private String itemNameCn;

    // 订单ID
    private Long orderID;

    // 款式ID
    private Long styleID;

    //款式面料ID
    private Long styleMaterialID;

    // 生产制单ID
    private Long productionID;

    // 生产制单No
    private String productionNo;

    // 合同ID
    private Long contractID;

    // 合同No
    private String contractNo;

    // 单耗状态
    private int unitCostStatus;

    // 订单品控状态区分
    private String orderStatus;

    // 内部订单号
    private String insideOrderNO;

    // 客户订单号
    private String customOrderNO;

    // 款式类别中文名称
    private String styleCategoryCN;

    // 款式中文名
    private String styleNameCn;

    // 物料ID
    private Long materialID;

    // 物料号
    private String materialCode;

    // 物料成分
    private String materialComposition;

    // 物料名称
    private String materialName;

    // 幅宽
    private String materialSpec;

    // 单耗
    private String materialUnitCost;

    // 损耗
    private String materialLoss;

    //换残
    private String changeImperfect;

    //单耗合计
    private String unitCostSum;

    //物料数量
    private String materialQuantity;

    //扣眼数
    private String holeCost;

    //主商标
    private String labelPosition;

    //面料标
    private String fabricComposition;

    //小号
    private String sizeLabel;

    //洗涤标
    private String careLabel;

    //产地标
    private String countryOfOrigin;

    //扣
    private String buttonSBU;

    //大扣数
    private String bigBtnNum;

    //小扣数
    private String smallBtnNum;

    //领子扣数
    private String scoBtnNum;

    //品控项目
    private String qcItem;

    //品控具体内容
    private String qcContent;

    //检查结果
    private String qcCheck;

    //图片路径
    private String filePath;

    //面料图片路径
    private String materialFile;

    //项目位置名称
    private String optionNameCn;

    //项目名称
    private String designNameCn;

    //尺码组CD
    private String styleGroupCD;

    // 开始日期
    private String beginDate;

    // 结束日期
    private String endDate;

    // 线号
    private String shirtSgd;

    // 订单状态CD
    private String orderStatusCD;

    // 生产号From
    private String styleNoFrom;

    // 生产号To
    private String styleNoTo;

    // 实际量体值
    private String fitValue;

    // 工序ID
    private String itemId;

    // 条形码code
    private String styleBarCode;

    // 备注
    private String cRemark;

    // 画面整合用ID
    private String strQcId;
    // 操作flg
    private String changeFlg;
    //  备注
    private String remarks;

    /**
     * 单耗
     *
     * @param sizeUnitCostList 服装款式信息
     * @return this
     */
    public StyleOptionEntity buildMaterialUnitCost(List<StyleOptionEntity> sizeUnitCostList) {

        if (!Strings.isNullOrEmpty(this.getOptionValue())) {
            StyleOptionEntity styleOptionEntity = returnUnitCost(sizeUnitCostList, this.itemCode + "-" + this.itemValue);
            if (styleOptionEntity != null) {
                this.setMaterialUnitCost(styleOptionEntity.getMaterialUnitCost());
            }
        }

        return this;
    }


    /**
     * @param itemCode    项目code
     * @param itemValue   项目value
     * @param optionValue 项目实际值
     */
    public StyleOptionEntity(
            String itemCode,
            String itemValue,
            String optionValue
    ) {
        this.itemCode = itemCode;
        this.itemValue = itemValue;
        this.optionValue = optionValue;
    }

    /**
     * 数据库取数据构造函数
     *
     * @param stringObjectMap resultset
     */
    public StyleOptionEntity(Map<String, Object> stringObjectMap) {
        this.itemCode = stringObjectMap.get("itemCode").toString();
        this.itemValue = stringObjectMap.get("itemValue").toString();
        this.materialUnitCost = stringObjectMap.get("materialUnitCost").toString();
        this.holeCost = stringObjectMap.get("holeCost").toString();
        this.remarks = stringObjectMap.get("remarks").toString();
    }

    public StyleOptionEntity(
            String itemCode,
            String itemValue,
            String materialUnitCost,
            String holeCost,
            String remarks
    ) {
        this.itemCode = itemCode;
        this.itemValue = itemValue;
        this.materialUnitCost = materialUnitCost;
        this.holeCost = holeCost;
        this.remarks = remarks;
    }

    /**
     * 扣的计算
     *
     * @return this
     */
    public StyleOptionEntity buildSBU(
            StyleDesignHistoryEntity cloudEntity,
            List<StyleOptionEntity> sizeUnitCostList,
            String dataSourceFlag
    ) {
        Map<String, Object> mapSbu = new HashMap<>();
        if ("ERP".equals(dataSourceFlag)) {
            mapSbu = countSbuNum(cloudEntity, sizeUnitCostList);

        } else if ("MTM".equals(dataSourceFlag)) {
            mapSbu = countSbuNumMTM(cloudEntity, sizeUnitCostList);
        }

        this.materialUnitCost = (Integer.parseInt(mapSbu.get("bigSbu").toString()) + Integer.parseInt(mapSbu.get("smallSbu").toString())) + "";
        this.remarks = mapSbu.get("remarkStr").toString();
        this.optionCost1 = mapSbu.get("smallSbu").toString() + "," + mapSbu.get("bigSbu").toString() + "," + mapSbu.get("scoCost").toString();
        this.scoBtnNum = mapSbu.get("scoCost").toString();
        return this;
    }

    /**
     * 扣眼颜色
     *
     * @param cloudEntity      款式历史记录
     * @param sizeUnitCostList 服装款式信息
     * @return this
     */
    public StyleOptionEntity buildSBH(
            StyleDesignHistoryEntity cloudEntity
            , List<StyleOptionEntity> sizeUnitCostList
    ) {
        String sbhStr = cloudEntity.getTrimSbh();
        if (!Strings.isNullOrEmpty(sbhStr) && sbhStr.contains("(")) {
            sbhStr = sbhStr.split("\\(")[1].replace(")", "");
        }
        this.setOptionValue(sbhStr);
        this.setRemarks(this.countBHNum(cloudEntity, sizeUnitCostList) + "");
        return this;
    }

    /**
     * 手绢
     *
     * @param cloudEntity 款式历史记录
     * @return this
     */
    public StyleOptionEntity buildSPO(StyleDesignHistoryEntity cloudEntity) {
        // 若手绢位置数据为空，则判断手绢配料位置数据，若有值，则根据制作方式设置数据，若为空则设置为无手绢
        if (Strings.isNullOrEmpty(cloudEntity.getModelSpo())) {
            if (!Strings.isNullOrEmpty(cloudEntity.getContSc9())) {
                if (cloudEntity.getModelSma().contains("4M") || cloudEntity.getModelSma().contains("SM")
                        || cloudEntity.getModelSma().contains("BM")) {
                    // 机缝手绢
                    this.setOptionValue("4POYES");
                } else if (cloudEntity.getModelSma().contains("4H") || cloudEntity.getModelSma().contains("SH")) {
                    // 手缝手绢
                    this.setOptionValue("4PHYES");
                }
            } else {
                // 无手绢
                this.setOptionValue("4PONO");
            }
        } else {
            this.setOptionValue(cloudEntity.getModelSpo());
        }
        return this;
    }

    /**
     * 小号
     *
     * @param cloudEntity    款式历史记录
     * @param dataSourceFlag 数据标识
     * @return this
     */
    public StyleOptionEntity buildSizeLabel(StyleDesignHistoryEntity cloudEntity, String dataSourceFlag) {
        if ("ERP".equals(dataSourceFlag)) {
            if (cloudEntity.getLablSize().toUpperCase().contains("YES") || cloudEntity.getLablSize().toUpperCase().contains("有")) {
                String orderStr = cloudEntity.getOrderNo().toLowerCase();
                if (orderStr.contains("sui") && !orderStr.contains("suitt")) {

                    this.setOptionValue("SU-" + cloudEntity.getShirtSize());
                    /*}else if (orderStr.indexOf("hq")!=-1) {*/
                } else {
                    if ("pacific-1".equals(cloudEntity.getLablSizeType().toLowerCase())) {
                        String lableSize = cloudEntity.getLablSize().toUpperCase().replace("YES / 有", "").trim();
                        this.setOptionValue(cloudEntity.getLablSizeType() + "-" + lableSize);
                    } else {
                        this.setOptionValue("LG-" + cloudEntity.getShirtSize());
                    }
                }
            }
        } else if ("MTM".equals(dataSourceFlag)) {
            if (!"".equals(cloudEntity.getLablSize()) && cloudEntity.getLablSize() != null) {
                this.setOptionValue(cloudEntity.getLablSize());
            }
        }
        return this;
    }

    /**
     * 洗涤成分
     *
     * @param cloudEntity 款式历史记录
     * @return this
     */
    public StyleOptionEntity buildCareLabel(StyleDesignHistoryEntity cloudEntity) {
        String clStr = cloudEntity.getLablCl();
        if (!"".equals(clStr)) {
            if (cloudEntity.getShopName().toUpperCase().contains("SUIT")) {
                clStr = "SUIT-" + clStr;
            } else if (cloudEntity.getFabricCode().toUpperCase().contains("KNIT")
                    || ResourcesUtil.getConfig("knit_other_fabric").contains(cloudEntity.getFabricCode().toUpperCase())) {
                clStr = "KNIT-" + clStr;
            } else if (!Strings.isNullOrEmpty(cloudEntity.getLablCoo()) && cloudEntity.getLablCoo().replace(" ", "").toUpperCase().contains("ROS")) {
                clStr = clStr + "-CA00572";
                // 洗涤种类
            } else if (!Strings.isNullOrEmpty(cloudEntity.getLablClt()) && cloudEntity.getLablClt().toLowerCase().contains("pacific")) {
                // 若洗涤种类为Pacific，则在后面拼接上洗涤成分
                clStr = cloudEntity.getLablClt() + "-" + clStr;
            }
        }
        this.setOptionValue(clStr);
        return this;
    }

    /**
     * 产地标
     *
     * @param cloudEntity    款式历史记录
     * @param dataSourceFlag 数据标识
     * @return this
     */
    public StyleOptionEntity buildCountry(StyleDesignHistoryEntity cloudEntity, String dataSourceFlag) {
        if ("ERP".equals(dataSourceFlag)) {
            // 正常订单
            if ("".equals(cloudEntity.getSampleFlag())) {
                if ("无产地标".equals(cloudEntity.getLablCoo().split("/")[1].trim())) {
                    this.setOptionValue("");
                } else {
                    this.setOptionValue(cloudEntity.getLablCoo().split("/")[1].trim());
                }
                // 小样订单
            } else {
                this.setOptionValue("");
            }
        } else if ("MTM".equals(dataSourceFlag)) {
            this.setOptionValue(cloudEntity.getLablCoo());
        }
        return this;
    }

    /**
     * 扣的计算(erp)
     *
     * @return 扣的计算结果
     */
    private Map<String, Object> countSbuNum(StyleDesignHistoryEntity cloudEntity, List<StyleOptionEntity> sizeUnitCostList) {
        Map<String, Object> map = new HashMap<>();
        StyleOptionEntity styleOption;
        //小扣
        int smallSbu = 0;
        //大扣
        int bigSbu = 0;
        //金属扣
        int metalSbu = 0;
        //备注
        String remarkStr = "";
        //领子扣
        String shirtScocellStr = cloudEntity.getModelSco();
        String scoCost = "0";
        if (!"".equals(shirtScocellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_sco") + "-" + shirtScocellStr);
            if (styleOption != null) {
                scoCost = styleOption.getMaterialUnitCost();
                smallSbu = smallSbu + (int) Double.parseDouble(scoCost);
            }
            //如果领子为4COCA3 2或4COBD3 2，大扣加2，否则加1
            /*if ("4COCA3 2,4COBD3 2".indexOf(shirtScocellStr) != -1) {
            	bigSbu = bigSbu + 2;
            }else {
            	bigSbu = bigSbu + 1;
            }*/
        }
        //袖开祺扣
        String shirtSvvcellStr = cloudEntity.getModelSsv();
        String ssvCost = "0";
        if (!"".equals(shirtSvvcellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_ssv") + "-" + shirtSvvcellStr);
            if (styleOption != null) {
                ssvCost = styleOption.getMaterialUnitCost();
            }
        }
        smallSbu = smallSbu + (int) Double.parseDouble(ssvCost);
        if (smallSbu != 0) {
            smallSbu = smallSbu + 1;
        }
        /*if ("4SV04".equals(shirtSvvcellStr)) {
    		if (shirtScocellStr.indexOf("4COBD") != -1 || shirtScocellStr.indexOf("4COBB") != -1) {
    			smallSbu = 3;
			}else {
				smallSbu = 0;
			 }
		}*/
        remarkStr += "小扣：" + smallSbu + " ";
        //袖头扣
        String shirtScucellStr = cloudEntity.getModelScu();
        String scuCost = "0";
        if (!"".equals(shirtScucellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_scu") + "-" + shirtScucellStr);
            if (styleOption != null) {
                scuCost = styleOption.getMaterialUnitCost();
            }
        }
        //后衣长
        String shirtLengthcellStr = cloudEntity.getSizeLen();
        //前立
        String shirtSfpcellStr = cloudEntity.getModelSfp();
        //前立扣数
        String shirtBofcellStr = cloudEntity.getSizeBof();
        int sfpCost = 0;
        if (!"".equals(shirtLengthcellStr)) {
            //如果前立为4FPA00、4FPC00、4FPB35或4FPB25，按照订单前立扣数数据取值。
            //如果前立为4FPPOL，则前立扣数为2，其它的前立扣数为4。
            //如果前立为4FPC00、4FPD00、4FPG00或4FPF00，则有4颗金属扣。
            if ("4FPD00,4FPG00,4FPF00,4FPPOP".contains(shirtSfpcellStr)) {
                sfpCost = 4;
                //sfpCost = (int)Double.parseDouble(shirtBofcellStr) - 4;
                if ("4FPD00,4FPG00,4FPF00".contains(shirtSfpcellStr)) {
                    metalSbu = 4;
                }
            } else if ("4FPPOL".equals(shirtSfpcellStr)) {
                //styleOption = returnUnitCost (sizeUnitCostList, ResourcesUtil.getConfig ("shirt_sfp") + "-" + shirtSfpcellStr);
	            /*if (styleOption != null) {
	            	sfpCost = (int)Double.parseDouble(styleOption.getMaterialUnitCost ());
	            }*/
                sfpCost = 2;
            } else {
                if ("4FPC00".equals(shirtSfpcellStr)) {
                    sfpCost = (int) Double.parseDouble(shirtBofcellStr);
                    //metalSbu = 4;
                } else {
                    sfpCost = (int) Double.parseDouble(shirtBofcellStr);
                }
            }
        }
        bigSbu = bigSbu + (int) Double.parseDouble(scuCost) + sfpCost;
        if (bigSbu != 0) {
            bigSbu = bigSbu + 1;
            remarkStr += "大扣：" + bigSbu;
        }
        if (metalSbu != 0) {
            remarkStr += " 金属扣：" + metalSbu;
        }
        map.put("bigSbu", bigSbu);
        map.put("smallSbu", smallSbu);
        map.put("remarkStr", remarkStr);
        map.put("scoCost", (int) Double.parseDouble(scoCost));
        return map;
    }


    /**
     * 扣的计算(MTM)
     *
     * @return 扣得计算结果
     */
    private Map<String, Object> countSbuNumMTM(StyleDesignHistoryEntity cloudEntity, List<StyleOptionEntity> sizeUnitCostList) {
        Map<String, Object> map = new HashMap<>();
        StyleOptionEntity styleOption;
        //小扣
        int smallSbu = 0;
        //大扣
        int bigSbu = 0;
        //金属扣
        int metalSbu = 0;
        //备注
        String remarkStr = "";
        //领子扣
        String shirtScocellStr = cloudEntity.getModelSco();
        String scoCost = "0";
        if (!"".equals(shirtScocellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_sco") + "-" + shirtScocellStr);
            if (styleOption != null) {
                scoCost = styleOption.getMaterialUnitCost();
                smallSbu = smallSbu + (int) Double.parseDouble(scoCost);
            }
        }
        //袖开祺扣
        String shirtSvvcellStr = cloudEntity.getModelSsv();
        String ssvCost = "0";
        if (!"".equals(shirtSvvcellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_ssv") + "-" + shirtSvvcellStr);
            if (styleOption != null) {
                ssvCost = styleOption.getMaterialUnitCost();
            }
        }
        smallSbu = smallSbu + (int) Double.parseDouble(ssvCost);
        if (smallSbu != 0) {
            smallSbu = smallSbu + 1;
        }
        remarkStr += "小扣：" + smallSbu + " ";
        //袖头扣
        String shirtScucellStr = cloudEntity.getModelScu();
        String scuCost = "0";
        if (!"".equals(shirtScucellStr)) {
            styleOption = returnUnitCost(sizeUnitCostList, ResourcesUtil.getConfig("shirt_scu") + "-" + shirtScucellStr);
            if (styleOption != null) {
                scuCost = styleOption.getMaterialUnitCost();
            }
        }
        //前身长
        String shirtYunLengthcellStr = cloudEntity.getSizeYunLen();
        //前立
        String shirtSfpcellStr = cloudEntity.getModelSfp();
        //前立扣数
        int sfpCost = 0;
        //余数
        double sfpCostRem;
        if (!"".equals(shirtYunLengthcellStr)) {
            //如果前立为4FPA00、4FPC00、4FPB35或4FPB25，按照原来算法计算大扣。
            //如果前立为4FPPOL，则前立扣数为2，其它的前立扣数为4。
            //如果前立为4FPC00、4FPD00、4FPG00或4FPF00，则有4颗金属扣。
            //$原来算法:前立扣数 = (前身长-5.5)/9 向下取整，如果(前身长-5.5)%9小于6，则减1。
            if ("4FPD00,4FPG00,4FPF00,4FPPOP".contains(shirtSfpcellStr)) {
                sfpCost = 4;
                if ("4FPD00,4FPG00,4FPF00".contains(shirtSfpcellStr)) {
                    metalSbu = 4;
                }
            } else if ("4FPPOL".equals(shirtSfpcellStr)) {
                sfpCost = 2;
            } else {
                sfpCost = (int) Math.floor((Double.parseDouble(shirtYunLengthcellStr) - 5.5) / 9);
                sfpCostRem = (Double.parseDouble(shirtYunLengthcellStr) - 5.5) % 9;
                if (sfpCostRem < 6) {
                    sfpCost = sfpCost - 1;
                }
				/*if ("4FPC00".equals(shirtSfpcellStr)) {
					metalSbu = 4;
				}*/
            }
        }
        bigSbu = bigSbu + (int) Double.parseDouble(scuCost) + sfpCost;
        if (bigSbu != 0) {
            bigSbu = bigSbu + 1;
            remarkStr += "大扣：" + bigSbu;
        }
        if (metalSbu != 0) {
            remarkStr += " 金属扣：" + metalSbu;
        }
        cloudEntity.setSizeBof(sfpCost + "");
        map.put("bigSbu", bigSbu);
        map.put("smallSbu", smallSbu);
        map.put("sfpCost", sfpCost);
        map.put("remarkStr", remarkStr);
        map.put("scoCost", (int) Double.parseDouble(scoCost));
        return map;
    }

    /**
     * 获取费用
     *
     * @param sizeUnitCost 单耗
     * @return 费用
     */
    private static StyleOptionEntity returnUnitCost(List<StyleOptionEntity> sizeUnitCost, String costStr) {

        for (StyleOptionEntity styleOption : sizeUnitCost) {
            if ((styleOption.getItemCode() + "-" + styleOption.getItemValue()).equals(costStr)) {

                return styleOption;
            }
        }
        return null;
    }

    /**
     * 扣眼数的计算
     *
     * @param cloudEntity      款式历史记录
     * @param sizeUnitCostList 服装款式信息
     * @return 数量
     */
    private int countBHNum(StyleDesignHistoryEntity cloudEntity, List<StyleOptionEntity> sizeUnitCostList) {
        //扣眼数
        int bHNum = 0;
        //领子
        String shirtScocellStr = cloudEntity.getModelSco();
        //袖头
        String shirtScucellStr = cloudEntity.getModelScu();
        //袖开祺
        String shirtSvvcellStr = cloudEntity.getModelSsv();
        //前立
        String shirtSfpcellStr = cloudEntity.getModelSfp();
        //前立扣数
        String shirtBofcellStr = cloudEntity.getSizeBof();
        if ("".equals(cloudEntity.getSampleFlag())) {
            bHNum = bHNum + (int) Double.parseDouble(shirtBofcellStr);
        } else {
            bHNum = 0;
        }
        for (StyleOptionEntity styleOption : sizeUnitCostList) {
            if (styleOption.getItemValue().equals(shirtScocellStr) ||
                    styleOption.getItemValue().equals(shirtScucellStr) ||
                    styleOption.getItemValue().equals(shirtSvvcellStr) ||
                    styleOption.getItemValue().equals(shirtSfpcellStr)) {
                bHNum = bHNum + (int) Double.parseDouble(styleOption.getHoleCost());
            }
        }
        return bHNum;
    }
}